$(function(){
    $("#send").click(function(){
        var phone = $("#phone").val();
        var yzm = $("#yzm").val();
        var pwd = $("#pwd").val();
        var rpwd = $("#rpwd").val();
        var user_name = $("#user_name").val();
        var number = $("#number").val()||"";
        var url = "http://119.23.107.233:4001";
        var register = "/mobile/register/";
        var judgephone = "/mobile/judgephone/";
        var uid ='';
        var data = {phone:phone,password:pwd,Number:number};
        if(!(/^1[34578]\d{9}$/.test(phone))){
            alert("手机号码有误，请重新输入！");
            return false;
        }
        if(pwd.length<6){
            alert("密码长度不能小于6位！");
            return false;
        }
        if(pwd !== rpwd){
            alert("两次密码输入不同，请重新输入");
            return false;
        }
        $.ajax({
            type: 'POST',
            url: url+judgephone,
            data: data,
            success: function(result) {
                if(result.result == "ok" ){
                    alert(result.message);
                    return false;
                }else{
                    $.ajax({
                        type: 'POST',
                        url: url+"/mobile/phone_sms/",
                        data: {phone:phone,code:yzm},
                        success: function(result) {
                            if(result){
                                $.ajax({
                                    type: 'POST',
                                    url: url+register,
                                    data: data,
                                    success: function(result) {
                                        alert(result.message);
                                        uid = result.uid;
                                        $.ajax({
                                            type: 'POST',
                                            url: url+"/mobile/updateuserinfo/",
                                            data: {uid:uid,userName:user_name},
                                            success: function(result) {
                                            },
                                            error:function(err){
                                                console.log(err);
                                            }
                                        });
                                    },
                                    error:function(err){
                                        console.log(err);
                                    }
                                });
                            }else{
                                alert("验证码错误，请重新输入！");
                            }
                        },
                        error:function(err){
                            console.log(err);
                        }
                    });
                }
            },
            error:function(err){
                console.log(err);
            }
        });
    });

    $('#s_yzm').click(function () {
        if(!(/^1[34578]\d{9}$/.test(phone))){
            alert("手机号码有误，请重新输入！");
            return false;
        }
        var flag = true;
        var n_phone = $("#phone").val();
        if(n_phone != ""){
            $.ajax({
                type: 'POST',
                url: "http://119.23.107.233:4001/mobile/judgephone/",
                data: {phone:n_phone},
                success: function(result) {
                    if(result.result == "ok" ){
                        alert(result.message);
                        return false;
                    }else{
                        flag = false;
                        $.ajax({
                            type: 'GET',
                            url: "http://119.23.107.233:4001/mobile/phone_sms/?phone="+n_phone+"&type=register",
                            success: function(result) {
                                alert("验证码发送成功，请注意查收！");
                            },
                            error:function(err){
                                console.log(err);
                            }
                        });
                    }
                },
                error:function(err){
                    console.log(err);
                }
            });
        }else{
            alert("请输入手机号！");
        }

        if(!flag){
            var count = 30;
            var countdown = setInterval(CountDown, 1000);
            function CountDown() {
                $("#s_yzm").attr("disabled", true);
                $("#s_yzm").text("重新发送 " + count );
                if (count == 0) {
                    $("#s_yzm").text("获取验证码").removeAttr("disabled");
                    clearInterval(countdown);
                }
                count--;
            }
        }
    });

});